<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\model\BaseModel;


class ModuleConfigModel extends BaseModel
{
    // 表名
    protected $name = 'tongcheng_module_config';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'open_tip_text',
        'open_advance_fee_text',
        'open_insure_text',
        'open_range_text',
        'open_weight_text',
        'open_surcharge_text',
        'open_appoint_time_text',
        'open_gender_text',
        'open_coupon_text',
        'open_pickup_code_text'
    ];

    protected $type = [
        'range_config'       => 'json',
        'weight_config'      => 'json',
        'surcharge_config'   => 'json',
        'claim_time_config'  => 'json',
        'claim_peak_config'  => 'json',
        'claim_money_config' => 'json',
    ];

    public function getOpenTipList()
    {
        return ['1' => __('Open_tip 1'), '0' => __('Open_tip 0')];
    }

    public function getOpenPrefeeList()
    {
        return ['1' => __('Open_advance_fee 1'), '0' => __('Open_advance_fee 0')];
    }

    public function getOpenInsureList()
    {
        return ['1' => __('Open_insure 1'), '0' => __('Open_insure 0')];
    }

    public function getOpenRangeList()
    {
        return ['1' => __('Open_range 1'), '0' => __('Open_range 0')];
    }

    public function getOpenWeightList()
    {
        return ['1' => __('Open_weight 1'), '0' => __('Open_weight 0')];
    }

    public function getOpenSurchargeList()
    {
        return ['1' => __('Open_surcharge 1'), '0' => __('Open_surcharge 0')];
    }

    public function getOpenGenderList()
    {
        return ['1' => __('Open_gender 1'), '0' => __('Open_gender 0')];
    }

    public function getOpenCouponList()
    {
        return ['1' => __('Open_coupon 1'), '0' => __('Open_coupon 0')];
    }

    public function getOpenPickupCodeList()
    {
        return ['1' => __('Open_pickup_code 1'), '0' => __('Open_pickup_code 0')];
    }

    public function getClaimModeList()
    {
        return ['fix' => __('claim_mode fix'), 'ratio' => __('claim_mode ratio')];
    }


    public function getOpenTipTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_tip']) ? $data['open_tip'] : '');
        $list  = $this->getOpenTipList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenAdvanceFeeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_advance_fee']) ? $data['open_advance_fee'] : '');
        $list  = $this->getOpenPrefeeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenInsureTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_insure']) ? $data['open_insure'] : '');
        $list  = $this->getOpenInsureList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenRangeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_range']) ? $data['open_range'] : '');
        $list  = $this->getOpenRangeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenWeightTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_weight']) ? $data['open_weight'] : '');
        $list  = $this->getOpenWeightList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenSurchargeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_surcharge']) ? $data['open_surcharge'] : '');
        $list  = $this->getOpenSurchargeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenAppointTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_appoint_time']) ? $data['open_appoint_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOpenGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_gender']) ? $data['open_gender'] : '');
        $list  = $this->getOpenGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenCouponTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_coupon']) ? $data['open_coupon'] : '');
        $list  = $this->getOpenCouponList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOpenPickupCodeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['open_pickup_code']) ? $data['open_pickup_code'] : '');
        $list  = $this->getOpenPickupCodeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setOpenAppointTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function module()
    {
        return $this->belongsTo('Module', 'module_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
